package com.linsen.erp.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsen.core.common.enums.DeleteType;
import com.linsen.core.common.util.ConvertUtil;
import com.linsen.core.mybatisplus.util.PageUtil;
import com.linsen.erp.admin.entity.PositionEntity;
import com.linsen.erp.admin.mapper.PositionMapper;
import com.linsen.erp.admin.provider.dto.PositionDTO;
import com.linsen.erp.admin.provider.qo.PositionQo;
import com.linsen.erp.admin.provider.vo.PositionVO;
import com.linsen.erp.admin.service.IPositionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 岗位 服务实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2024-10-08
 */
@Service
@RequiredArgsConstructor
public class PositionServiceImpl extends ServiceImpl<PositionMapper, PositionEntity> implements IPositionService {

    /** 封装分页列表查询的wrapper对象 */
    private Wrapper<PositionEntity> getWrapper(PositionQo positionQo){
        String name = positionQo.getName();

        return new LambdaQueryWrapper<PositionEntity>()
                .like(ObjectUtil.isNotEmpty(name),PositionEntity::getName,name)
                .eq(PositionEntity::getIsDeleted,DeleteType.NORMAL.getValue())
                .orderByAsc(PositionEntity::getPriority)
                .orderByDesc(PositionEntity::getCreateTime);
    }

    @Override
    public Page<PositionVO> page(PositionQo qo) {
        return PageUtil.convert(this.page(new Page<>(qo.getCurrent(), qo.getSize()), getWrapper(qo)),PositionVO.class);
    }

    @Override
    public PositionVO get(String id) {
        return ConvertUtil.convert(this.getById(id), PositionVO.class);
    }

    @Transactional
    @Override
    public void savePosition(PositionDTO positionDTO) {
        this.save(ConvertUtil.convert(positionDTO, PositionEntity.class));
    }

    @Transactional
    @Override
    public void updatePosition(PositionDTO positionDTO) {
        this.updateById(ConvertUtil.convert(positionDTO, PositionEntity.class));
    }

    @Transactional
    @Override
    public void delete(String id) {
        this.update(
                new UpdateWrapper<PositionEntity>().lambda()
                        .set(PositionEntity::getIsDeleted,DeleteType.DELETED.getValue())
                        .eq(PositionEntity::getId,id)
        );
    }
}
